/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaAulesPanel.java
 *
 * Created on 27-nov-2011, 17:45:30 */
package edu.uoc.tdp.pac4.only5.gui.client.panels.aules;

import edu.uoc.tdp.pac4.only5.gui.client.panels.AbstractPanel;
import edu.uoc.tdp.pac4.only5.gui.client.model.SpaceComboBoxModel;
import edu.uoc.tdp.pac4.only5.gui.client.model.UserComboBoxModel;
import edu.uoc.tdp.pac4.only5.gui.client.model.UserBookingVOTableModel;
import edu.uoc.tdp.pac4.only5.gui.client.model.ResourceBookingVOTableModel;

import edu.uoc.tdp.pac4.only5.domain.Booking;
import edu.uoc.tdp.pac4.only5.domain.Space;
import edu.uoc.tdp.pac4.only5.domain.User;
import edu.uoc.tdp.pac4.only5.domain.EnumStateBooking;
import edu.uoc.tdp.pac4.only5.domain.Payment;

import edu.uoc.tdp.pac4.only5.gui.client.ClientSession;

import edu.uoc.tdp.pac4.only5.manager.BookingManager;
import edu.uoc.tdp.pac4.only5.manager.SpaceManager;
import edu.uoc.tdp.pac4.only5.manager.UserManager;
import edu.uoc.tdp.pac4.only5.manager.ResourceManager;
import edu.uoc.tdp.pac4.only5.manager.PaymentManager;

import edu.uoc.tdp.pac4.only5.exceptions.EespaisDatabaseException;

import edu.uoc.tdp.pac4.only5.vo.UserBookingVO;
import edu.uoc.tdp.pac4.only5.vo.ResourceBookingVO;

import edu.uoc.tdp.pac4.only5.tools.DateTool;

import java.awt.Component;
import java.rmi.RemoteException;
 

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ListSelectionModel;
import java.util.Date;
import java.util.ResourceBundle;
import javax.swing.JTable;
 
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

/**
 *
 * @author rafael correal
 * Consulta de las peticiones por estado,espacio y cliente, con la opcion a cancelarlas
 * si estan en situacion para ello
 */
public class ConsultaAulesPanel extends AbstractPanel {
    
    private static ResourceBundle resourceBundle;
    private BookingManager bookingManager;
    private SpaceManager spaceManager;
    private UserManager userManager;
    private ResourceManager resourceManager;
    private PaymentManager paymentManager;
    
    private SpaceComboBoxModel spaceComboBoxModel;
    private UserComboBoxModel  userComboBoxModel;
    
    

    /** Creates new form ConsultaAulesPanel */
    /**
     * Consulta bookings
     * @param bookingManager
     * @param spaceManager
     * @param userManager
     * @param resourceManager 
     * @param payment 
     */
    public ConsultaAulesPanel(BookingManager bookingManager, SpaceManager spaceManager,UserManager userManager,
            ResourceManager resourceManager,PaymentManager paymentManager) 
    {
        initComponents();
        this.spaceManager = spaceManager;
        this.bookingManager = bookingManager;
        this.userManager = userManager;
        this.resourceManager = resourceManager;
        this.paymentManager = paymentManager;
        
        resourceBundle = ResourceBundle.getBundle("edu.uoc.tdp.pac4.only5.gui.client.Client");
        
        setLabels();
        fillTable();
        fillComboBox();
        fillComboBox1();
        fillComboBox2();
        tbl_bookings.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    /**
     * Rellemamos la tabla con el resultado de la consulta
     * 
     */
    private void fillTable() {
        Date from = null;
        Date to = null;
         

        Integer space = null;
        String cliente=null;
        String estado=null;
        
        if (cmbSpaces != null && cmbSpaces.getSelectedItem() != null) {
            space = ((Space) cmbSpaces.getSelectedItem()).getSpaceId();
            
        }

        if (cmbClientes != null && cmbClientes.getSelectedItem() != null) {
            cliente = ((User) cmbClientes.getSelectedItem()).getUserId();
        }
        
         if (cmbEstados != null) {
            if (cmbEstados.getSelectedItem() !=null){
                estado = cmbEstados.getSelectedItem().toString();
                if (estado == "TOTS"){
                    estado =null;
                }
            }
        }
        
        List<UserBookingVO> bookings = null;
                
        try {
        bookings = (List<UserBookingVO>) bookingManager.getBookingsVO(cliente, space, from, to,estado);
        } catch (RemoteException ex) {
            Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        String[] columns = {"table.booking.id", "aules.Espai","aules.SpaceDescription","table.booking.userId", "table.user.name","tabla.user.surname1","table.booking.startTime", "table.booking.endTime", "table.booking.pax", "table.booking.status", "table.booking.creationTime", "table.booking.cancelTime", "table.booking.cancelUser", "table.booking.recordDate"};
        tbl_bookings.setModel(new UserBookingVOTableModel(bookings, columns));

        setTableHeaders(columns);
        fillListResources(-1);
    }
    /*
     * Rellenamos el combo de Espacios
     */
    private void fillComboBox() {
        try 
        {
            spaceComboBoxModel = new SpaceComboBoxModel(spaceManager.getSpaces());
            cmbSpaces.setModel(spaceComboBoxModel);
        } 
        catch (EespaisDatabaseException ex) {
            Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
        }        catch (RemoteException ex) 
        {
            Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /*
     * Rellenamos el combo de clientes
     */
 private void fillComboBox1() {
        try 
        {
            userComboBoxModel = new UserComboBoxModel(userManager.getUsersByRol(3));
            cmbClientes.setModel(userComboBoxModel);
        } 
        catch (RemoteException ex) 
        {
            Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 /*
  * Rellenamos el combo de Estados
  */
  private void fillComboBox2() {

      cmbEstados.removeAllItems();
      cmbEstados.addItem("TOTS");
      cmbEstados.addItem(EnumStateBooking.PENDENT);
      cmbEstados.addItem(EnumStateBooking.CANCELADA);
      cmbEstados.addItem(EnumStateBooking.CURSADA);
            
   /*
    * establecemos cabeceras en los grids de consulta
   */
    }
    private void setTableHeaders(String[] headers) {
       
        String[] traducedHeaders = getResourceBundleTool().getHeaders(headers);
        for (int i = 0; i < traducedHeaders.length; i++) 
        {
            packColumn(tbl_bookings,  i, 2);
            tbl_bookings.getColumnModel().getColumn(i).setHeaderValue(traducedHeaders[i]);
        }
    }
    
    private void setTableHeadersBR(String[] headers) {
        String[] traducedHeaders = getResourceBundleTool().getHeaders(headers);
        for (int i = 0; i < traducedHeaders.length; i++) {
            tbl_bookings_resources.getColumnModel().getColumn(i).setHeaderValue(traducedHeaders[i]);
        }
    }
   

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cmbSpaces = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_bookings = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cmbEstados = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        cmbClientes = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbl_bookings_resources = new javax.swing.JTable();
        jLabel27 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        jLabel29 = new javax.swing.JLabel();
        jLabel30 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();
        jLabel32 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        jLabel1.setText("Cerca de Reserves");

        tbl_bookings.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbl_bookings.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tbl_bookings.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbl_bookingsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbl_bookings);

        jLabel2.setText("Espai");

        jLabel3.setText("Estat");

        cmbEstados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbEstadosActionPerformed(evt);
            }
        });

        jLabel6.setText("Client");

        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("jLabel4");

        jLabel5.setText("jLabel5");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("jLabel7");

        jLabel8.setText("jLabel8");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel9.setText("jLabel9");

        jLabel10.setText("jLabel10");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel11.setText("jLabel11");

        jLabel12.setText("jLabel12");

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel13.setText("jLabel13");

        jLabel14.setText("jLabel14");

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel15.setText("jLabel15");

        jLabel16.setText("jLabel16");

        jLabel17.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel17.setText("jLabel17");

        jLabel18.setText("jLabel18");

        jLabel19.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel19.setText("jLabel19");

        jLabel20.setText("jLabel20");

        jLabel21.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel21.setText("jLabel21");

        jLabel22.setText("jLabel22");

        jLabel23.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel23.setText("jLabel23");

        jLabel24.setText("jLabel24");

        jLabel25.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel25.setText("Datos de la Reserva");

        jLabel26.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel26.setText("Recursos Aula");

        tbl_bookings_resources.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tbl_bookings_resources);

        jLabel27.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel27.setText("jLabel27");

        jLabel28.setText("jLabel28");

        jLabel29.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel29.setText("Pagos");

        jLabel30.setText("jLabel30");

        jLabel31.setText("jLabel31");

        jLabel32.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel32.setText("Causas Anulacion");

        jButton2.setText("jButton2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jFormattedTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jFormattedTextField1FocusLost(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 918, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cmbClientes, 0, 239, Short.MAX_VALUE)
                            .addComponent(cmbSpaces, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmbEstados, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel13)
                                    .addComponent(jLabel17)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel15)
                                    .addComponent(jLabel19)
                                    .addComponent(jLabel21)
                                    .addComponent(jLabel23)
                                    .addComponent(jLabel27))
                                .addGap(56, 56, 56)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
                                    .addComponent(jLabel20, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                                    .addComponent(jLabel22, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel24, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel28))
                                .addGap(63, 63, 63))
                            .addComponent(jLabel25, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel26)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel29, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(42, 42, 42)
                                .addComponent(jLabel30, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(80, 80, 80)
                                .addComponent(jLabel31))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2))
                            .addComponent(jLabel32, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(23, 23, 23)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cmbClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbSpaces, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmbEstados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel25)
                    .addComponent(jLabel26))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel13)
                            .addComponent(jLabel14)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel16)
                            .addComponent(jLabel15))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel17)
                            .addComponent(jLabel18))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel19)
                            .addComponent(jLabel20))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel22)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel24)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel28))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel21)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel23)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel27))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabel31))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel29)
                                    .addComponent(jLabel30))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel32)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2))))
                .addContainerGap(54, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cmbEstadosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbEstadosActionPerformed
        // TODO:Gerard, add your handling code here:
    }//GEN-LAST:event_cmbEstadosActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        fillTable();     }//GEN-LAST:event_jButton1ActionPerformed

    /*  
     * cargamos en pantalla el booking seleccionado con click del raton
     * En funcion del estado de la reserva activaremos el boton de cancelar la reserva 
     */
    private void tbl_bookingsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbl_bookingsMouseClicked
        
        
        int fila = tbl_bookings.rowAtPoint(evt.getPoint());
        int columna = tbl_bookings.columnAtPoint(evt.getPoint());
        
        jLabel5.setText(String.valueOf(tbl_bookings.getValueAt(fila,0)));
        jLabel8.setText(String.valueOf(tbl_bookings.getValueAt(fila,1)) + " " + tbl_bookings.getValueAt(fila, 2));
        jLabel10.setText(String.valueOf(tbl_bookings.getValueAt(fila,3)) + " " + tbl_bookings.getValueAt(fila, 4)+ " " + tbl_bookings.getValueAt(fila, 5) );
        jLabel12.setText(String.valueOf(tbl_bookings.getValueAt(fila,6)));
        jLabel14.setText(String.valueOf(tbl_bookings.getValueAt(fila,7)));
        jLabel16.setText(String.valueOf(tbl_bookings.getValueAt(fila,8)));
        jLabel18.setText(String.valueOf(tbl_bookings.getValueAt(fila,9)));
        jLabel20.setText(String.valueOf(tbl_bookings.getValueAt(fila,10)));
        jLabel22.setText(String.valueOf(tbl_bookings.getValueAt(fila,11)));
        jLabel24.setText("-");
        jLabel22.setText("-");
        if (tbl_bookings.getValueAt(fila,12) !=null)
        {
            jLabel24.setText(String.valueOf(tbl_bookings.getValueAt(fila,12))); 
        }
         if (tbl_bookings.getValueAt(fila,11) !=null)
        {
            jLabel22.setText(String.valueOf(tbl_bookings.getValueAt(fila,11))); 
        }
        jLabel28.setText(String.valueOf(tbl_bookings.getValueAt(fila,13)));
        
        jButton2.setEnabled(true);
             
        int idB = Integer.parseInt(jLabel5.getText());
        Date fecha;
        fecha= DateTool.getInstance().getSqlDate(new Date());
        /*
         * buscamos la reserva
         */
        Booking  bk = new Booking ();
        try {
            bk = bookingManager.getBooking(idB);
        } catch (RemoteException ex) {
            Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        // tenemos reserva: En funcion del estado activamos o desactivamos el boton de cancelar 
        // la reserva
        if (bk.getId() != null)
        {
            if (bk.getStatus().equals("CANCELADA"))
            {
                jButton2.setEnabled(false);
                jFormattedTextField1.setText(bk.getCancelReason());
            }
            else 
            {
                jButton2.setEnabled(true);
                jFormattedTextField1.setText("-");
            
                if (bk.getStartTime().before(fecha))
                {
                     jButton2.setEnabled(false);
                    jFormattedTextField1.setText("-");
                }
                    if (bk.getStartTime().after(fecha))
                {
                    jButton2.setEnabled(true);
                    jFormattedTextField1.setText("-");
                }
            }  
        }
        else
        {
                jButton2.setEnabled(true);
                jFormattedTextField1.setText("-");
        }
        /*
         * mostramos los pagos
         */
        Payment pay = new Payment();
        try {
            try {
                pay = paymentManager.getPaymentbyId(idB);
            } catch (EespaisDatabaseException ex) {
                Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (RemoteException ex) {
            Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        jLabel30.setText("-");
        jLabel31.setText("-");
        fillListResources(idB);  
        if(pay !=null ){
            jLabel30.setText(String.valueOf(pay.getDate_charged()));
            jLabel31.setText(String.valueOf(pay.getPrice()));
        }
    }//GEN-LAST:event_tbl_bookingsMouseClicked
    /*
     * boton de cancelacion
     * Cancelamos la reserva y calculamos el recargo en el pago
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String t1 = jFormattedTextField1.getText();
        if ("-".equals(String.valueOf(t1)))
            // if (String.valueOf(t1) == "-")
        {
           showErrorMessageDialog(this, "aules.error.causa");
        }
        else
        {
           int idB = Integer.parseInt(jLabel5.getText());
           if (idB !=0)
           {
                try {
                     Payment pay = new Payment();
                     try {
                        try {
                            pay = paymentManager.getPaymentbyId(idB);
                        } catch (EespaisDatabaseException ex) {
                            Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
                        }
                         } 
                     catch (RemoteException ex) {
                             Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
                         }
                     String causa = String.valueOf(t1);
                     Booking Bk = new Booking();
                     Bk = bookingManager.getBooking(idB);
                     try {
                         String sUser  = ClientSession.getInstance().getUser().getUserId();
                         if(pay !=null)
                         {
                            int recargo = bookingManager.calculaRecargo(Bk, pay);
                            if (recargo !=0)
                            {
                                paymentManager.UpdatePaymentBooking(idB, recargo);
                            }
                         }
                         bookingManager.UpdateCancelBooking(idB,causa,sUser);
                         showMessageDialog(this,"aules.cancelok");
                         fillTable();
                      
                     } catch (EespaisDatabaseException ex) {
                        showErrorMessageDialog(this, "error.generic.databaseerror");
                    } catch (RemoteException ex) {
                        showErrorMessageDialog(this, "error.generic.rmi");
                     }
                } catch (RemoteException ex) {
                    Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
           } 
           else 
           {
                showErrorMessageDialog(this, "aules.error.selecionarid");
           }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jFormattedTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField1FocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_jFormattedTextField1FocusLost
    /*
     * cargamos en pantalla los recursos de una reserva
     */
    private void fillListResources(int bookingId){
  
        List<ResourceBookingVO> resources = null;
        try {
            resources= (List<ResourceBookingVO>) resourceManager.getResourceBooking(bookingId);
        } catch (RemoteException ex) {
            Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EespaisDatabaseException ex) {
                Logger.getLogger(ConsultaAulesPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        String[] columns = {"aules.resource.descripcion","aules.resource.precio","aules.resource.precioH","aules.resource.booked","aules.resource.total"};

        tbl_bookings_resources.setModel(new ResourceBookingVOTableModel(resources, columns,false));
          
        
       setTableHeadersBR(columns);
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbClientes;
    private javax.swing.JComboBox cmbEstados;
    private javax.swing.JComboBox cmbSpaces;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbl_bookings;
    private javax.swing.JTable tbl_bookings_resources;
    // End of variables declaration//GEN-END:variables
    /*
     * 
     *establecemos los valores iniciales de las etiquetas de pantalla
     * 
     */
    @Override
 public void setLabels() {
        jLabel4.setText(getText("aules.Reserva"));
        jLabel7.setText(getText("aules.Espai"));
        jLabel9.setText(getText("aules.Cliente"));
        jLabel11.setText(getText("aules.Inicio"));
        jLabel13.setText(getText("aules.Final"));
        jLabel15.setText(getText("aules.Pax"));
        jLabel17.setText(getText("aules.Status"));
        jLabel19.setText(getText("aules.FechaCreacion"));
        jLabel21.setText(getText("aules.FechaCancelacion"));
        jLabel23.setText(getText("aules.CancelUser"));
        jLabel27.setText(getText("aules.FechaAlta"));
        jLabel5.setText(getText("aules.Limpiar"));
        jLabel8.setText(getText("aules.Limpiar"));
        jLabel10.setText(getText("aules.Limpiar"));
        jLabel12.setText(getText("aules.Limpiar"));
        jLabel14.setText(getText("aules.Limpiar"));
        jLabel16.setText(getText("aules.Limpiar"));
        jLabel18.setText(getText("aules.Limpiar"));
        jLabel20.setText(getText("aules.Limpiar"));
        jLabel22.setText(getText("aules.Limpiar"));
        jLabel24.setText(getText("aules.Limpiar"));
        jLabel28.setText(getText("aules.Limpiar"));
        jButton1.setText(getText("aules.Buscar"));
        jLabel6.setText(getText("aules.Cliente"));
        jLabel2.setText(getText("aules.Espai"));
        jLabel3.setText(getText("aules.Status"));
        jLabel25.setText(getText("aules.DadesReserva"));
        jLabel26.setText(getText("aules.RecursosAula"));
        jLabel29.setText(getText("aules.Pagos"));
        jLabel30.setText(getText("aules.Limpiar"));
        jLabel31.setText(getText("aules.Limpiar"));
        jLabel32.setText(getText("aules.Cancel1"));
        jButton2.setText(getText("aules.Cancelar"));
    }
    /*
     * buscamos los valores de las etiquetas
     */
    @Override
    public  String getText(String key) {
        return resourceBundle.getString(key);
    }
    /*
     * Funcion para montar los anchos de las columnas del jTable
     */
public void packColumn(JTable table, int vColIndex, int margin) {
        TableModel model = table.getModel();
        DefaultTableColumnModel colModel = (DefaultTableColumnModel)table.getColumnModel();
        TableColumn col = colModel.getColumn(vColIndex);
        int width = 0;             
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) 
        {
            renderer = table.getTableHeader().getDefaultRenderer();
        }
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
        width = comp.getPreferredSize().width;             
        for (int r=0; r<table.getRowCount(); r++) 
        {
            renderer = table.getCellRenderer(r, vColIndex);
            comp = renderer.getTableCellRendererComponent(
                table, table.getValueAt(r, vColIndex), false, false, r, vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
        }
        width += 2*margin;            
        col.setPreferredWidth(width);
    }


}
